//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    banner:[],
    store:[],type:'',
    template:[],show:false,coupon:'',title:'',id:'',pid:'',uid:'',fff:'',store1:""
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
 onShow(){
   
   
   this.getceshi()
   console.log(this.data.fff);
   
  let uid=wx.getStorageSync('uid')?wx.getStorageSync('uid'):''
  this.setData({uid:uid})
  console.log(uid)
  if(uid==''&&this.data.pid!=''){
    console.log(111);
    
    wx.navigateTo({
      url: '/pages/mine/loginuser/loginuser?pid='+this.data.pid+"&id="+this.data.id,
    })
  }
  if(uid==''&&this.data.id!=''){
    console.log(2222);
    
    wx.navigateTo({
      url: '/pages/mine/loginuser/loginuser?pid='+this.data.pid+"&id="+this.data.id,
    })
  }
  if(uid!=''&&this.data.id!=''){
    console.log(uid,this.data.id);
    
    wx.request({
      url:'https://zujieba.com/Api/bind/bind.html',
      data:{pid:this.data.id,uid},
      method:'POST',
      success:(res)=>{
        console.log(res);
        
        if(res.data.success==1){
           wx.showToast({title:'绑定成功'})
           this.setData({id:''})
        }else{
          console.log(res);
          this.setData({id:''})
          
        }
      }
    })}
  
  if(app.globalData.coupon.length!=0){
    this.setData({
      show:true,
      coupon:app.globalData.coupon
    })
  }
  app.globalData.coupon=[]
  this.getshopshare()

 },
 remove1(){
     this.setData({show:false})
 },
  onLoad: function (options) {

       this.getsystem()
   
  
      
      console.log(options);
      
       console.log(options.pid);
       let pid=options.pid?options.pid:''
       
       
      this.setData({pid})
      if (options.scene){
 
        const scene = decodeURIComponent(options.scene)
        console.log(scene)  // 22&shopname
   
        var id = scene.substring(4)
        console.log(id);
        
        
        
        
        this.setData({id:id})
        wx.setStorageSync('id',id)
      }  
      
  
    
   
    this.getlocation()
    
    var that = this
    wx.request({
      url: 'https://zujieba.com/api/index/index.html', //仅为示例，并非真实的接口地址
      data: {
      },
      success: function(res) {
        console.log(res);
        that.setData({
          banner : res.data.data.banner,
          store : res.data.data.store,
          template : res.data.data.template,
        })
        }
    })


    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  //店铺详情
  store_xq:function(e){
    console.log(e.currentTarget.id);
    
    var id = e.currentTarget.id;
    
    
    wx.navigateTo({
      url: '../store/store_xq?id='+id,
    })
  },
  getlocation(){
    const that =this
   wx.getSetting({
     success: (result)=>{
       console.log(result);
      if(!result.authSetting['scope.userLocation']) {
        wx.authorize({
          scope: 'scope.userLocation',
          success: (result)=>{
            console.log(result);
            wx.getLocation({
              type:'gcj02',
              altitude: 'altitude',
              success:(res)=>{
                console.log(res);
                wx.setStorageSync('lng', res.longitude)
                wx.setStorageSync('lat', res.latitude)
                this.getnearshop1(res.longitude,res.latitude)

              }
            })
            
           that.setData({
             show:false
           })
            
          },
        
         
        });
      }else{
       wx.getLocation({
         altitude: 'altitude',
         type:'gcj02',
         success:(res)=>{
           console.log(res);
           wx.setStorageSync('lng', res.longitude)
           wx.setStorageSync('lat', res.latitude)
           this.getnearshop1(res.longitude,res.latitude)
         }
       })
      }
       
     },
    

     
   });
    
           
                  
 },
 //跳转到文件打印       新
      navfileprint(){
        console.log(this.data.store1.id,this.data.goods_id);
        
        if(this.data.uid==''){
          wx.navigateTo({
            url: '/pages/mine/loginuser/loginuser?id='+this.data.id,
          })
        }else{
          console.log(this.data.store1);
          
          if(!this.data.store1){
            wx.showToast({
              title: '请授权位置',
            })
            return
          }
         wx.navigateTo({
           url: '/pages/fileprint/fileprint?near_id='+this.data.store1.id+'&goods_id='+this.data.goods_id,
         })}
      },
      //跳转到图片打印
      navindexphoto(){
        if(this.data.uid==''){
          wx.navigateTo({
            url: '/pages/mine/loginuser/loginuser?id='+this.data.id,
          })
        }else{
          if(!this.data.store1){
            wx.showToast({
              title: '请授权位置',
            })
            return
          }
        wx.navigateTo({
          url: '/pages/index/photoprint/photo?near_id='+this.data.store1.id+'&goods_id='+this.data.goods_id,
        })}
      },
      //跳转到商户推荐
      navnear(e){
        if(this.data.uid==''){


          wx.navigateTo({
            url: '/pages/mine/loginuser/loginuser?id='+this.data.id,
          })
        }else{
          wx.navigateTo({
            url: '/pages/index/near/near?statu='+e.currentTarget.dataset.statu,
          })
        }
        
      },
         //跳转到商户推荐
         navnearheader(e){
          if(this.data.uid==''){
  
  
            wx.navigateTo({
              url: '/pages/mine/loginuser/loginuser?id='+this.data.id,
            })
          }else{
            wx.navigateTo({
              url: '/pages/index/near/near?statu='+e.currentTarget.dataset.statu+'&page=indexheader',
            })
          }
          
        },
     
      //跳转到我要合作
      navhezuo(){
        if(this.data.uid==''){
          wx.navigateTo({
            url: '/pages/mine/loginuser/loginuser?id='+this.data.id,
          })
        }else{
        wx.navigateTo({
          url: '/pages/mine/authen/authen',
        })}
      },
      //跳转到回收设备
      navhuishou(){
        if(this.data.uid==''){
          wx.navigateTo({
            url: '/pages/mine/loginuser/loginuser?id='+this.data.id,
          })
        }else{
        wx.navigateTo({
          url: '/pages/hebinprint/hebinprint',
        })}
      },
      onPullDownRefresh(){
         
        wx.request({
          url: 'https://zujieba.com/api/index/index.html', //仅为示例，并非真实的接口地址
          data: {
          },
          success: (res)=>{
            console.log(res);
            this.setData({
              banner : res.data.data.banner,
              store : res.data.data.store,
              template : res.data.data.template,
            })
              wx.stopPullDownRefresh()
            }
        })
           
      },
     //获取用户是否是店家还是共享商  在uid有的情况下 获取
     getshopshare(){
      wx.request({
        url: 'https://zujieba.com/Api/member/member.html',
        data:{uid:wx.getStorageSync('uid')},
        method:'POST',
        success:(res)=>{
          if(res.data.success==2){return}
          console.log(res);
          let type=res.data.data.type
          this.setData({
            type
          })
          
        }
      })

   },
 
 navhoutai(){
    wx.navigateTo({
      url: '/pages/mine/store_admin/index?type='+this.data.type,
    })
 },
 navshop(){
   wx.navigateTo({
     url: '/pages/shop/shop',
   })
 },
 onShareAppMessage(){
  let pid=wx.getStorageSync('uid')
  return{title:'慧享印',path:'/pages/index/index?pid='+pid,imageUrl:'../../img/mingpian.jpg'}
   
},
navcoupon(){
  wx.navigateTo({
    url: '/pages/mine/newcoupon?statu=1',
  })
},
getceshi(){
  wx.request({
    url: 'https://cloud.wisiyilink.com/api/print/job',
    method:'GET',
    header:{ApiKey:'ueQr4lbU5lurtgz6D0xilLMcHErzDBOb'},
    data:{deviceId:'ps01cz01526843',deviceKey:'HWgtLbhd7BH4vWTl',devicePort:1,task_id:'a2c10168-6f76-4013-9c75-c513cfe4206a'},
    success:(res)=>{
      console.log(res);
      
    },
    fail:(res)=>{
      console.log(res);
      
    }
  })
},
navshop(){
  wx.navigateToMiniProgram({
    appId: 'wxdfb9573399073ad7',
    path: 'pages/home/my/classlist/classlist?lat='+wx.getStorageSync('lat')+'&lng='+wx.getStorageSync('lng'),
   
    envVersion: 'release',
    success(res) {
      // 打开成功
    }
  })
},
whyprint(){
  wx.navigateTo({
    url: '/pages/whyprint/whyprint',
  })
},
// 获取设备导航栏高度

 getsystem(){
    wx.getSystemInfo({
      complete: (res) => {
        console.log(res);
        let menubutton= wx.getMenuButtonBoundingClientRect() 
        console.log(menubutton.height);
       let barheight = res.statusBarHeight +menubutton.height + (menubutton.top-res.statusBarHeight )*2
       console.log(barheight);
        this.setData({
          barheight,
          inheight:barheight-(barheight-menubutton.top-menubutton.height)
        })
      },
    })
 },
 navpage(e){
   console.log(e);
   if(e.currentTarget.dataset.type==1){
     wx.previewImage({
       urls: [e.currentTarget.dataset.banner],
     })
  }
   if(e.currentTarget.dataset.type==2){
    wx.navigateTo({
      url: e.currentTarget.dataset.link,
    })
   }else if(e.currentTarget.dataset.type==3){
      wx.navigateToMiniProgram({
    appId: 'wxdfb9573399073ad7',
    path: 'pages/home/my/classlist/classlist?lat='+wx.getStorageSync('lat')+'&lng='+wx.getStorageSync('lng'),
   
    envVersion: 'release',
    success(res) {
      // 打开成功
    }
  })
   }
      
 },

 //获取位置近列表
 getnearshop1(lng,lat){
  wx.request({
    url: 'https://zujieba.com/api/store/list_store.html',
   data:{distance:1,lng,lat},
    success:(res)=>{
         console.log(res);
         
         let store1=res.data.data
         this.getgoodslist(store1[0].id)
         store1.forEach((item)=>{item.long=(item.long/1000).toFixed(1)})
         console.log(store1);
         this.setData({
          
           store1 : store1[0]
           
         })
         
    }
  })
},
//获取设备列表
getgoodslist(near_id){
  wx.request({
    url: 'https://zujieba.com/Api/store/goods.html',
    data:{pid:near_id},
    method:'POST',
    success:(res)=>{
      console.log(res);
      
      let goods_id=res.data.data[0].id

    this.setData({
      goods_id,
      
    })
   
    }
  })
},

})
